package com.xiaomiMall.back.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.xiaomiMall.back.domain.Member;
import com.xiaomiMall.back.service.Impl.MemberServiceImpl;
import com.xiaomiMall.mange.domain.vo.Notepad;
import com.xiaomiMall.mange.domain.vo.OrderShow;
import com.xiaomiMall.mange.service.Impl.MallUserServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
public class MemberController extends BaseController {

    @Autowired
    MemberServiceImpl memberService;

    @Autowired
    MallUserServiceImpl userService;

    // 获取会员list
    @GetMapping("/getMember")
    public TableDataInfo getMember() {
        startPage();
        List<Member> list = memberService.getAllMember();
        return getDataTable(list);
    }

    //获取消息通知list
    @PostMapping("/getNotepad")
    public AjaxResult getNotepad(@RequestBody HashMap<String,Long> sender) {
        AjaxResult ajax = AjaxResult.success();
        Long senderId = sender.get("senderId");
        List<Notepad> notepadList = memberService.selectNotepadBySenderId(senderId);
        ajax.put("notepadList", notepadList);
        return ajax;
    }

    // 更新消息通知的状态
    @PostMapping("/handleCharge")
    public AjaxResult handleCharge(@RequestBody HashMap<String, Long> note) {
        AjaxResult ajax = AjaxResult.success();
        Long id = note.get("notepadId");
        Long status = note.get("status");
        String message = "";
        if (status == 2) {
            message = memberService.updateNotepadStatus(id, status) ? "已拒绝该申请" : "拒绝失败，请稍后再试";
        } else if (status == 1) {
            if (memberService.updateNotepadStatus(id, status)) {
                message = "已同意该申请";
                Notepad notepad = memberService.selectNotepadByid(id);
                int wallet = Integer.parseInt(notepad.getMessage().substring(4));
                memberService.updateUserWallet(notepad.getSenderId(),wallet);
                message += "，充值成功";
            } else {
                message = "同意失败，请稍后再试";
            }
        }
        ajax.put("message", message);
        return ajax;
    }

    // 重置密码
    @PostMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestBody HashMap<String, Long> data) {
        AjaxResult ajax = AjaxResult.success();
        String message = "";
        String newPassword = memberService.resetPassword(data.get("userId"));
        if (StringUtils.isNotNull(newPassword)) {
            message = "重置成功，新密码是："+ newPassword;
        } else {
            message = "重置失败，请稍后重试";
        }
        ajax.put("message", message);
        return ajax;
    }

    // 搜索会员
    @GetMapping("/getSearch")
    public TableDataInfo getSearch(@Param("email") String email) {
        startPage();
        List<Member> list = memberService.selectMemberByEmail(email);
        return getDataTable(list);
    }

    // 获取需要发货的订单
    @PostMapping("/getFahuo")
    public AjaxResult getFahuo() {
        AjaxResult ajax = AjaxResult.success();
        List<OrderShow> orderList = userService.getOrderShow(null, 1);
        ajax.put("orderList",orderList);
        return ajax;
    }

    // 发货需要发货的订单
    @PostMapping("/sendHuo")
    public AjaxResult sendHuo(@RequestBody HashMap<String,String> data) {
        AjaxResult ajax = AjaxResult.success();
        String message = userService.updateOrderStatus(data.get("orderNum"), 0) ? "发货成功" : "发货失败";
        ajax.put("message", message);
        return ajax;
    }

}
